package com.example.demo.common.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.entity.Func;
import com.example.demo.common.mapper.FuncMapper;
import com.example.demo.common.service.FuncService;
import com.example.demo.common.utils.IpUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
@CrossOrigin
@RestController
@RequestMapping("/func")
public class FuncController {
    @Resource
    FuncService funcService;
    @Resource
    FuncMapper funcMapper;
    @PostMapping
    public Result<?> save(@RequestBody Func func){
        funcService.saveFunc(func);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody Func func){
        funcService.updateFunc(func);
        return Result.success();
    }
    @DeleteMapping("/{ref}")
    public Result<?> update(@PathVariable String ref){
        funcService.deleteByRef(ref);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Func> wrapper = Wrappers.<Func>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Func::getRef, search);
        }
        Page<Func> funcPage = funcMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(funcPage);
    }
}
